package com.sw.study.springwork.service.impl;

import com.sw.study.springwork.annotation.Autowired;
import com.sw.study.springwork.annotation.Service;
import com.sw.study.springwork.annotation.Transactional;
import com.sw.study.springwork.dao.AccountDao;
import com.sw.study.springwork.pojo.Account;
import com.sw.study.springwork.service.AccountService;

/**
 * Created by sw on 2020/4/6.
 */
@Service
@Transactional
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    // 构造函数传值/set方法传值

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }
    /**
     * 转账方法
     *
     * @param fromNo 转出账号
     * @param toNo   转入账号
     * @param money 金额
     */
    @Override
    public void transfer(String fromNo, String toNo, int money) throws Exception {
        Account from = accountDao.queryAccountByCardNo(fromNo);
        Account to = accountDao.queryAccountByCardNo(toNo);

        from.setMoney(from.getMoney() - money);
        to.setMoney(to.getMoney() + money);

        accountDao.updateAccountByCardNo(to);
        int i=100/0;
        accountDao.updateAccountByCardNo(from);
    }
}